package biblioGraphiqueV2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;

import java.util.HashMap;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.JList;
import java.awt.Color;


public class VueResOuvrage extends Vue{
	private Lecteur lecteur;
	private Ouvrage ouvrage;
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textFieldNom;
	private JTextField textFieldPrenom;
	private JButton btnChercher;
	private JButton btnAnnuler;
	private JButton btnAnnuler_1;
	private JButton btnReserver;
	private DefaultListModel model;
	private DefaultListModel model2;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JList liste;
	private JList list;
	private JTextField textFieldReservationRestante;
	/**
	 * Constructeur de la fenêtre
	 */
	public VueResOuvrage(Controleur controleur){
		
		super(controleur);
		setTitle("Réservation Ouvrage");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		setBounds(100, 100, 477, 428);
		contentPane = new JPanel();
		this.addWindowListener (new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
			getControleur().fermerVue(VueResOuvrage.this);
			}
		});		
		
		model = new DefaultListModel();
		alimenteListeLec();
		liste = new JList(model);
		liste.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, new Color(0, 0, 0), null, null, null));
		liste.setSelectionMode(0);
		liste.setBounds(277, 14, 178, 82);
		contentPane.add(liste);
		
		
		
		scrollPane = new JScrollPane(liste);
		scrollPane.setBounds(277, 14, 178, 82);

		contentPane.add(scrollPane);
		

		
		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Numero Lecteur : ");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(16, 19, 243, 15);
		contentPane.add(lblNewLabel);
		
		btnChercher = new JButton("Chercher");
		btnChercher.setBounds(229, 118, 107, 25);
		contentPane.add(btnChercher);
		btnChercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// liaison de la vue avec l'objet observé
				setLecteur(getControleur().rechLecteur((String)liste.getSelectedValue()));
		}});
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.setBounds(348, 118, 107, 25);
		contentPane.add(btnAnnuler);
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			getControleur().fermerVue(VueResOuvrage.this);
			}
		});
		
		model2 = new DefaultListModel();
		list = new JList(model2);
		list.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, new Color(0, 0, 0), null, null, null));
		list.setSelectionMode(0);
		contentPane.add(list);
		
		scrollPane_1 = new JScrollPane(list);
		scrollPane_1.setBounds(275, 278, 180, 60);
		contentPane.add(scrollPane_1);

		
		JSeparator separator = new JSeparator();
		separator.setBounds(86, 155, 340, 2);
		contentPane.add(separator);
		
		textFieldNom = new JTextField();
		textFieldNom.setBounds(278, 169, 177, 19);
		contentPane.add(textFieldNom);
		textFieldNom.setColumns(10);
		
		JLabel lblNom = new JLabel("Nom :");
		lblNom.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNom.setBounds(17, 171, 243, 15);
		contentPane.add(lblNom);
		
		textFieldPrenom = new JTextField();
		textFieldPrenom.setBounds(277, 200, 177, 19);
		contentPane.add(textFieldPrenom);
		textFieldPrenom.setColumns(10);
		
		JLabel lblPrenom = new JLabel("Prenom :");
		lblPrenom.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPrenom.setBounds(17, 202, 243, 15);
		contentPane.add(lblPrenom);
		
		JLabel lblIsbn = new JLabel("Isbn :");
		lblIsbn.setHorizontalAlignment(SwingConstants.RIGHT);
		lblIsbn.setBounds(17, 283, 243, 15);
		contentPane.add(lblIsbn);
		
		btnAnnuler_1 = new JButton("Annuler");
		btnAnnuler_1.setBounds(348, 365, 107, 25);
		contentPane.add(btnAnnuler_1);
		btnAnnuler_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			getControleur().fermerVue(VueResOuvrage.this);
			}
		});
		
		btnReserver = new JButton("Reserver");
		btnReserver.setBounds(229, 365, 107, 25);
		contentPane.add(btnReserver);
		
		JLabel lblReservationResatante = new JLabel("Réservation Restante :");
		lblReservationResatante.setHorizontalAlignment(SwingConstants.RIGHT);
		lblReservationResatante.setBounds(73, 235, 187, 15);
		contentPane.add(lblReservationResatante);
		
		textFieldReservationRestante = new JTextField();
		textFieldReservationRestante.setBounds(277, 233, 178, 19);
		contentPane.add(textFieldReservationRestante);
		textFieldReservationRestante.setColumns(10);
		
		btnReserver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			getControleur().resOuvrage((String)liste.getSelectedValue(),(String)list.getSelectedValue());
			}});
	}
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			textFieldNom.setEditable(false);
			textFieldReservationRestante.setEditable(false);
			textFieldPrenom.setEditable(false);
			btnChercher.setEnabled(true);
			btnAnnuler.setEnabled(true);
			btnAnnuler_1.setEnabled(false);
			btnReserver.setEnabled(false);
			list.setEnabled(false);
			break;
			}
		case finale: {
			btnChercher.setEnabled(false);
			btnAnnuler.setEnabled(false);
			btnAnnuler_1.setEnabled(true);
			btnReserver.setEnabled(true);
			liste.setEnabled(false);
			list.setEnabled(true);
			break;
			}

		}
	}
	
	public void alimenteLec(Lecteur lec, Quota quot){
		textFieldNom.setText(lec.getNom());
		textFieldPrenom.setText(lec.getPrenom());
		
		int i = lec.getEmprunt().size();
		float lim = quot.getMaxReservation();
	
		textFieldReservationRestante.setText(String.valueOf(lim - i));
		
	}
	
	public void alimenteListeLec(){
		HashMap<String, Lecteur> lec;
		lec = getControleur().getLecteurs();
		
		Vector<String> vec = new Vector<String>(); 

		for (String mapKey : lec.keySet()) {
			String num = lec.get(mapKey).getNumLecteur();
			vec.add(num);
		}
		
		for(int i = vec.size()-1 ; i >= 0 ; i--)
		{
			model.addElement(vec.elementAt(i));
		}
	}	
	public void alimenteIsbn(){
		HashMap<String, Ouvrage> ouv;
		ouv = getControleur().getOuvrages();
		Vector<String> vec = new Vector<String>(); 
		
		for (String mapKey : ouv.keySet()) {
			String num = ouv.get(mapKey).getIsbn();
			vec.add(num);
		}
		
		for(int i = vec.size()-1 ; i >= 0 ; i--)
		{
			model2.addElement(vec.elementAt(i));
		}
	}
	private void setLecteur(Lecteur lect) {
		 lecteur = lect;
	}
	private Lecteur getLecteur(){
		return lecteur;
	}
	private Ouvrage getOuvrage(){
		return  ouvrage;
	}
	
}